package com.yanyi.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.yanyi.music.domain.Comment;
import com.yanyi.music.service.impl.CommentServiceImpl;
import com.yanyi.music.utils.Consts;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 评论控制类
 */
@Api(tags = "留言API")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;

    /**
     * 添加评论
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addComment(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //用户id
        String userId = request.getParameter("userId");
        //评论类型（0歌曲1歌单）
        String type = request.getParameter("type");
        //歌曲id
        String songId = request.getParameter("songId");
        //歌单id
        String songListId = request.getParameter("songListId");
        //评论内容
        String content = request.getParameter("content").trim();
        //保存到评论的对象中
        Comment comment = new Comment();
        comment.setUserId(Integer.parseInt(userId));
        comment.setType(new Byte(type));
        if (new Byte(type) == 0) {
            comment.setSongId(Integer.parseInt(songId));
        } else {
            comment.setSongListId(Integer.parseInt(songListId));
        }
        comment.setContent(content);
        int flag = commentService.insert(comment);
        if (flag == 1) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "评论成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "评论失败");
        return jsonObject;
    }

    /**
     * 修改评论
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateComment(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //主键
        String id = request.getParameter("id").trim();
        //用户id
        String userId = request.getParameter("userId").trim();
        //评论类型（0歌曲1歌单）
        String type = request.getParameter("type").trim();
        //歌曲id
        String songId = request.getParameter("songId").trim();
        //歌单id
        String songListId = request.getParameter("songListId").trim();
        //评论内容
        String content = request.getParameter("content").trim();

        //保存到评论的对象中
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setUserId(Integer.parseInt(userId));
        comment.setType(new Byte(type));
        if (songId != null && songId.equals("")) {
            songId = null;
        } else {
            comment.setSongId(Integer.parseInt(songId));
        }
        if (songListId != null && songListId.equals("")) {
            songListId = null;
        } else {
            comment.setSongListId(Integer.parseInt(songListId));
        }
        comment.setContent(content);

        int flag = commentService.updateByPrimaryKeySelective(comment);
        if (flag == 1) {   //保存成功
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除评论
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteComment(HttpServletRequest request) {
        String id = request.getParameter("id").trim();
        int flag = commentService.deleteByPrimaryKey(Integer.parseInt(id));
        return flag > 0 ? true : false;
    }

    /**
     * 根据主键查询整个对象
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        //主键
        String id = request.getParameter("id").trim();
        return commentService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有评论
     */
    @RequestMapping(value = "/allComment", method = RequestMethod.GET)
    public Object allComment(HttpServletRequest request) {
        return commentService.findAllByMap(null);
    }

    /**
     * 查询某个歌曲下的所有评论
     */
    @RequestMapping(value = "/commentOfSongId", method = RequestMethod.GET)
    public Object commentOfSongId(HttpServletRequest request) {
        //歌曲id
        String songId = request.getParameter("songId");
        Map<String, Object> map = new HashMap<>();
        map.put("songId", songId);
        return commentService.findAllByMap(map);
    }

    /**
     * 查询某个歌单下的所有评论
     */
    @RequestMapping(value = "/commentOfSongListId", method = RequestMethod.GET)
    public Object commentOfSongListId(HttpServletRequest request) {
        //歌曲id
        String songListId = request.getParameter("songListId");
        Map<String, Object> map = new HashMap<>();
        map.put("songListId", songListId);
        return commentService.findAllByMap(map);
    }

    /**
     * 给某个评论点赞
     */
    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public Object like(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        //主键
        String id = request.getParameter("id").trim();
        //用户id
        String up = request.getParameter("up").trim();
        //保存到评论的对象中
        Comment comment = new Comment();
        comment.setId(Integer.parseInt(id));
        comment.setUp(Integer.parseInt(up));
        int flag = commentService.updateByPrimaryKeySelective(comment);
        if (flag == 1) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "点赞成功");
            return jsonObject;
        }
        jsonObject.put(Consts.CODE, 0);
        jsonObject.put(Consts.MSG, "点赞失败");
        return jsonObject;
    }
}
